<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\User;
use App\Models\UserAttribute;
use App\Models\UserGrowing;
use App\Models\EnrollTeacher;
use Illuminate\Support\Facades\DB;
class SyncTeacherInfoTousers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:teacher_info_to_user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步老师信息到用户表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $teachers = EnrollTeacher::get();
        DB::beginTransaction();
        try{
            foreach($teachers as $teacher){
                $user  = User::where('mobile',$teacher->mobile)->first();
                if($user){
                    $userAttr = UserAttribute::where('user_id',$user->id)->first();
                    if(!$userAttr->is_fission){
                        $userAttr->is_fission = 1;
                        $userAttr->save();
                    }
                    $userGrow = UserGrowing::where('user_id',$user->id)->first();
                    if($userGrow){
                        UserGrowing::where('user_id',$user->id)->update(['fission_id'=>0]);
                    }else{
                        $userGrow = new UserGrowing();
                        $userGrow->fission_id = 0;
                        $userGrow->user_id = $user->id;
                        $userGrow->name = $user->name;
                        $userGrow->mobile = $user->mobile;
                        $userGrow->msg = '咨询老师批量导入';
                        $userGrow->save();
                    }
                }else{
                    $user = new User();
                    $user->mobile = $teacher->mobile;
                    $user->password = bcrypt(md5($teacher->mobile));
                    $user->name = $teacher->name;
                    if($user->save()){
                        $userAttr = new UserAttribute();
                        $userAttr->is_fission = 1;
                        $userAttr->remark = '招生老师';
                        $userAttr->user_id = $user->id;
                        if($userAttr->save()){
                            $userGrow = new UserGrowing();
                            $userGrow->fission_id = 0;
                            $userGrow->user_id = $user->id;
                            $userGrow->name = $user->name;
                            $userGrow->mobile = $user->mobile;
                            $userGrow->msg = '咨询老师批量导入';
                            $userGrow->save();
                        }
                    }
                }
            }
            DB::commit();
        }catch(\Exception $e){
            DB::rollback();
            logger()->info($e->getMessage());
        }
        $this->info('完成');
        return ;
    }
}